package com.shengyijie.util;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream; 
import java.util.Calendar;
import java.util.Date;

import android.app.Activity;
import android.content.pm.ApplicationInfo;
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix; 
import android.graphics.PixelFormat;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Environment;
 

public class ImageUtility {

	 public static int MAX_WIDTH=612;
	 public static int MAX_HEIGHT=612;
	 
	 public static Bitmap DrawableToBitmap(Drawable bitmapd) {
	    	BitmapDrawable bd = (BitmapDrawable) bitmapd; 
	        return bd.getBitmap();
	    }
	 public static Drawable BitmapToDrawable(Bitmap bitmapd) {
	    	return new BitmapDrawable(bitmapd);
	    }
	 
	 public static Bitmap resizeImage(Bitmap bmp) {  	  
   	 // Bitmap BitmapOrg =bmp;

   	  int width = bmp.getWidth();
   	  int height = bmp.getHeight();
   	  if(width<=MAX_WIDTH&&height<=MAX_HEIGHT){
   		  return bmp;
   	  }
   	  
   	  int newWidth;
 	  int newHeight;
 	  float scal;
   	  if(width>height){
   		 // if(width>=)
   		newWidth=MAX_WIDTH;
   		scal=width/MAX_WIDTH;
   		newHeight=(int) (height/scal);
   	  }else{
   		 newHeight=MAX_HEIGHT;
   		 scal=height/MAX_HEIGHT;
   		 newWidth=(int) (width/scal);
   	  }
   	  

   	  // calculate the scale
   	  float scaleWidth = ((float) newWidth) / width;
   	  float scaleHeight = ((float) newHeight) / height;

   	  // create a matrix for the manipulation
   	  Matrix matrix = new Matrix();
   	  // resize the Bitmap
   	  matrix.postScale(scaleWidth, scaleHeight);
   	  // if you want to rotate the Bitmap
   	  // matrix.postRotate(45);

   	  // recreate the new Bitmap
   	  Bitmap resizedBitmap = Bitmap.createBitmap(bmp, 0, 0,
   	                                             width, height, matrix, true);
   	  
   	  if(!bmp.isRecycled()){
   		   bmp.recycle();
 	  }
   	
   	  return resizedBitmap;

   }
	 
	 public static Bitmap resizeImage(Bitmap bmp,int max) {  	  
	   	 // Bitmap BitmapOrg =bmp;

	   	  int width = bmp.getWidth();
	   	  int height = bmp.getHeight();
	   	  if(width<=max&&height<=max){
	   		  return bmp;
	   	  }
	   	  
	   	  int newWidth;
	 	  int newHeight;
	 	  float scal;
	   	  if(width>height){
	   		 // if(width>=)
	   		newWidth=max;
	   		scal=width/max;
	   		newHeight=(int) (height/scal);
	   	  }else{
	   		 newHeight=max;
	   		 scal=height/max;
	   		 newWidth=(int) (width/scal);
	   	  }
	   	  

	   	  // calculate the scale
	   	  float scaleWidth = ((float) newWidth) / width;
	   	  float scaleHeight = ((float) newHeight) / height;

	   	  // create a matrix for the manipulation
	   	  Matrix matrix = new Matrix();
	   	  // resize the Bitmap
	   	  matrix.postScale(scaleWidth, scaleHeight);
	   	  // if you want to rotate the Bitmap
	   	  // matrix.postRotate(45);

	   	  // recreate the new Bitmap
	   	  Bitmap resizedBitmap = Bitmap.createBitmap(bmp, 0, 0,
	   	                                             width, height, matrix, true);
	   	  
//	   	  if(!bmp.isRecycled()){
//	   		   bmp.recycle();
//	 	  }
	   	
	   	  return resizedBitmap;

	   }
   
   public static Bitmap resizeImage(Bitmap BitmapOrg, int w, int h) {

   	 

   	  int width = BitmapOrg.getWidth();
   	  int height = BitmapOrg.getHeight();
   	  int newWidth = w;
   	  int newHeight = h;

   	  // calculate the scale
   	  float scaleWidth = ((float) newWidth) / width;
   	  float scaleHeight = ((float) newHeight) / height;

   	  // create a matrix for the manipulation
   	  Matrix matrix = new Matrix();
   	  // resize the Bitmap
   	  matrix.postScale(scaleWidth, scaleHeight);
   	  Bitmap resizedBitmap = Bitmap.createBitmap(BitmapOrg, 0, 0,
   	                                             width, height, matrix, true);
   	  
   	  return  resizedBitmap;

   }
   
   
   
   public static void savePicture(Bitmap bm){		
		try {
			if(bm!=null){
				String imageLow="upload_low.jpg";
				String imageStandar="upload_standar.jpg";
				FileOutputStream fileOutputStream = null;
				 
				if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {   
					 File destDir = new File(Environment.getExternalStorageDirectory(),"diipo");
					  if (!destDir.exists()) {
					    destDir.mkdirs();
					  }
			           /**  
			         * Environment.getExternalStorageDirectory();  
			         * �õ���洢�豸��·��  
			         *   
			         */  
		             File file = new File(Environment.getExternalStorageDirectory(),"diipo/"+imageStandar);
		             fileOutputStream = new FileOutputStream(file);   
					 bm.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream);
					 fileOutputStream.flush();
					 fileOutputStream.close();
					 
					 file = new File(Environment.getExternalStorageDirectory(),"diipo/"+imageLow);
		             fileOutputStream = new FileOutputStream(file);   
		             bm=ImageUtility.resizeImage(bm,400, 400);
					 bm.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream);
					 fileOutputStream.flush();
					 fileOutputStream.close();
					 
//					 if(!bm.isRecycled()){
//						 bm.recycle();
//					 } 
				}
			}
		} catch (Exception e) {
		}
	}
   
   
   public static void saveImage(String url,Drawable d){		
		try {
			if(url.length()>0&&d!=null){
				String fileNameString=url;
				FileOutputStream fileOutputStream = null;
				if (d != null) {
					if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {   
						 File destDir = new File(Environment.getExternalStorageDirectory(),"diipo");
						  if (!destDir.exists()) {
						    destDir.mkdirs();
						  }
				           /**  
				         * Environment.getExternalStorageDirectory();  
				         * �õ���洢�豸��·��  
				         *   
				         */  
			            File file = new File(Environment.getExternalStorageDirectory(),"diipo/"+fileNameString);
			            fileOutputStream = new FileOutputStream(file);  
			            BitmapDrawable bd = (BitmapDrawable) d;
						Bitmap bm = bd.getBitmap();
						bm.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream);
						fileOutputStream.flush();
						fileOutputStream.close();
		           
		    	   }
				}
			}
		} catch (Exception e) {}
	}
   
   public static String getPictureFileName(String url){
	   String name="";
	   try{
		    String[] paramstrs = url.split("/");
			String temp=paramstrs[paramstrs.length-1];
			String[] paramstr = temp.split("\\.");
			name=paramstr[0];
	   }catch(Exception e){}
	   return name;
   }
   public static byte[] drawableToBytes(Drawable drawable) {  
       
       Bitmap bitmap = Bitmap  
                       .createBitmap(  
                                       drawable.getIntrinsicWidth(),  
                                       drawable.getIntrinsicHeight(),  
                                       drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888  
                                                       : Bitmap.Config.RGB_565);  
       Canvas canvas = new Canvas(bitmap);  
       //canvas.setBitmap(bitmap);  
       drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());  
       drawable.draw(canvas);  
       ByteArrayOutputStream baos = new ByteArrayOutputStream();    
       bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);    
	    return baos.toByteArray();
  }  
   
   public static String getTimeGap(String createTime) {  
	   try{
		   Date startDate=new Date(Long.parseLong(createTime)*1000);
		   Date endDate=new Date(System.currentTimeMillis());
	       Calendar fromCalendar = Calendar.getInstance();  
	       fromCalendar.setTime(startDate);  
	       fromCalendar.set(Calendar.HOUR_OF_DAY, 0);  
	       fromCalendar.set(Calendar.MINUTE, 0);  
	       fromCalendar.set(Calendar.SECOND, 0);  
	       fromCalendar.set(Calendar.MILLISECOND, 0);  
	 
	       Calendar toCalendar = Calendar.getInstance();  
	       toCalendar.setTime(endDate);  
	       toCalendar.set(Calendar.HOUR_OF_DAY, 0);  
	       toCalendar.set(Calendar.MINUTE, 0);  
	       toCalendar.set(Calendar.SECOND, 0);  
	       toCalendar.set(Calendar.MILLISECOND, 0);  
	 
	       int minutes=(int) ((toCalendar.getTime().getTime() - fromCalendar.getTime().getTime()) / (1000 * 60));
	       
	       if(minutes<60){
	    	   return minutes+"����֮ǰ";
	       }else if(minutes<60*24){
	    	   return (minutes/60)+"Сʱ֮ǰ";
	       }else{
	    	   return (minutes/60/24)+"��֮ǰ";
	       }
	    }catch(Exception e){
	    	return "";
	    }
   } 
}
